#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class CourseArrangeProvider : CY.GFive.Core.Providers.DALProvider.ICourseArrangeProvider
	{
		#region Sql
		private static readonly string SqlSelectCourseArrange = "SELECT [Id], [TeacherCode],[ClassRoomCode],[StdYear],[Semester],[GradeCode],[ProfCode],[ClassCode],[WeekDay],[Lesson],[CourseCode],[CourseCate] FROM [CourseArrange] WHERE  [Id]=@Id";

		private static readonly string SqlInsertCourseArrange = "INSERT INTO [CourseArrange]([TeacherCode],[ClassRoomCode],[StdYear],[Semester],[GradeCode],[ProfCode],[ClassCode],[WeekDay],[Lesson],[CourseCode],[CourseCate]) values(@TeacherCode,@ClassRoomCode,@StdYear,@Semester,@GradeCode,@ProfCode,@ClassCode,@WeekDay,@Lesson,@CourseCode,@CourseCate);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateCourseArrange = "UPDATE [CourseArrange] SET [TeacherCode] = @TeacherCode,[ClassRoomCode] = @ClassRoomCode,[StdYear] = @StdYear,[Semester] = @Semester,[GradeCode] = @GradeCode,[ProfCode] = @ProfCode,[ClassCode] = @ClassCode,[WeekDay] = @WeekDay,[Lesson] = @Lesson,[CourseCode] = @CourseCode,[CourseCate] = @CourseCate WHERE  [Id]=@Id";

		private static readonly string SqlDeleteCourseArrange = "DELETE [CourseArrange] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllCourseArrange = "SELECT [Id], [TeacherCode],[ClassRoomCode],[StdYear],[Semester],[GradeCode],[ProfCode],[ClassCode],[WeekDay],[Lesson],[CourseCode],[CourseCate] FROM [CourseArrange]";
		#endregion

		#region ICourseArrangeProvider Members
		public Core.Business.CourseArrange Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectCourseArrange);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.CourseArrange courseArrange = new Core.Business.CourseArrange();

				if (!reader.IsDBNull(0)) courseArrange.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) courseArrange.TeacherCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) courseArrange.ClassRoomCode = reader.GetString(2);
				if (!reader.IsDBNull(3)) courseArrange.StdYear = reader.GetString(3);
				if (!reader.IsDBNull(4)) courseArrange.Semester = reader.GetInt32(4);
				if (!reader.IsDBNull(5)) courseArrange.GradeCode = reader.GetString(5);
				if (!reader.IsDBNull(6)) courseArrange.ProfCode = reader.GetString(6);
				if (!reader.IsDBNull(7)) courseArrange.ClassCode = reader.GetString(7);
				if (!reader.IsDBNull(8)) courseArrange.WeekDay = reader.GetInt32(8);
				if (!reader.IsDBNull(9)) courseArrange.Lesson = reader.GetInt32(9);
				if (!reader.IsDBNull(10)) courseArrange.CourseCode = reader.GetString(10);
				if (!reader.IsDBNull(11)) courseArrange.CourseCate = reader.GetString(11);

				return courseArrange;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.CourseArrange courseArrange)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@TeacherCode", SqlDbType.NVarChar, courseArrange.TeacherCode);
			sql.AddParameter("@ClassRoomCode", SqlDbType.NVarChar, courseArrange.ClassRoomCode);
			sql.AddParameter("@StdYear", SqlDbType.NVarChar, courseArrange.StdYear);
			sql.AddParameter("@Semester", SqlDbType.Int, courseArrange.Semester);
			sql.AddParameter("@GradeCode", SqlDbType.NVarChar, courseArrange.GradeCode);
			sql.AddParameter("@ProfCode", SqlDbType.NVarChar, courseArrange.ProfCode);
			sql.AddParameter("@ClassCode", SqlDbType.NVarChar, courseArrange.ClassCode);
			sql.AddParameter("@WeekDay", SqlDbType.Int, courseArrange.WeekDay);
			sql.AddParameter("@Lesson", SqlDbType.Int, courseArrange.Lesson);
			sql.AddParameter("@CourseCode", SqlDbType.NVarChar, courseArrange.CourseCode);
			sql.AddParameter("@CourseCate", SqlDbType.NVarChar, courseArrange.CourseCate);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertCourseArrange);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) courseArrange.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.CourseArrange courseArrange)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@TeacherCode", SqlDbType.NVarChar, courseArrange.TeacherCode);
			sql.AddParameter("@ClassRoomCode", SqlDbType.NVarChar, courseArrange.ClassRoomCode);
			sql.AddParameter("@StdYear", SqlDbType.NVarChar, courseArrange.StdYear);
			sql.AddParameter("@Semester", SqlDbType.Int, courseArrange.Semester);
			sql.AddParameter("@GradeCode", SqlDbType.NVarChar, courseArrange.GradeCode);
			sql.AddParameter("@ProfCode", SqlDbType.NVarChar, courseArrange.ProfCode);
			sql.AddParameter("@ClassCode", SqlDbType.NVarChar, courseArrange.ClassCode);
			sql.AddParameter("@WeekDay", SqlDbType.Int, courseArrange.WeekDay);
			sql.AddParameter("@Lesson", SqlDbType.Int, courseArrange.Lesson);
			sql.AddParameter("@CourseCode", SqlDbType.NVarChar, courseArrange.CourseCode);
			sql.AddParameter("@CourseCate", SqlDbType.NVarChar, courseArrange.CourseCate);
			sql.AddParameter("@Id", SqlDbType.Int, courseArrange.Id);
			sql.ExecuteSql(SqlUpdateCourseArrange);
		}

		public void Delete(Core.Business.CourseArrange courseArrange)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, courseArrange.Id);
			sql.ExecuteSql(SqlDeleteCourseArrange);
		}

		public IList<Core.Business.CourseArrange> GetAllCourseArrange()
		{
			IList<Core.Business.CourseArrange> courseArrangelist = new List<Core.Business.CourseArrange>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllCourseArrange);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.CourseArrange courseArrange = new Core.Business.CourseArrange();

					if (!reader.IsDBNull(0)) courseArrange.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) courseArrange.TeacherCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) courseArrange.ClassRoomCode = reader.GetString(2);
					if (!reader.IsDBNull(3)) courseArrange.StdYear = reader.GetString(3);
					if (!reader.IsDBNull(4)) courseArrange.Semester = reader.GetInt32(4);
					if (!reader.IsDBNull(5)) courseArrange.GradeCode = reader.GetString(5);
					if (!reader.IsDBNull(6)) courseArrange.ProfCode = reader.GetString(6);
					if (!reader.IsDBNull(7)) courseArrange.ClassCode = reader.GetString(7);
					if (!reader.IsDBNull(8)) courseArrange.WeekDay = reader.GetInt32(8);
					if (!reader.IsDBNull(9)) courseArrange.Lesson = reader.GetInt32(9);
					if (!reader.IsDBNull(10)) courseArrange.CourseCode = reader.GetString(10);
					if (!reader.IsDBNull(11)) courseArrange.CourseCate = reader.GetString(11);

					courseArrange.MarkOld();
					courseArrangelist.Add(courseArrange);
				}
				reader.Close();
			}
			return courseArrangelist;
		}
		#endregion
	}
}
